using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;

namespace 地图哇哇哇.地图
{
    public class 测试层 : NotifyPropertyChanged
    {
        private string _名称;
        private string _编号;
        private 地图尺寸 _尺寸;
        private Brush _填充画笔;
        private ObservableCollection<测试区域> _测试区域们 = new ObservableCollection<测试区域>();
        private bool _显示层格线;
        public long 生成耗时 { get; set; }

        [NotifyParentProperty(true)]
        public bool 显示层格线
        {
            get => _显示层格线;
            set => Set(ref _显示层格线, value);
        }

        public string 名称
        {
            get => _名称;
            set => _名称 = value;
        }

        public string 编号
        {
            get => _编号;
            set => _编号 = value;
        }

        public 地图尺寸 尺寸
        {
            get => _尺寸;
            set => _尺寸 = value;
        }

        public Brush 填充画笔
        {
            get => _填充画笔;
            set => _填充画笔 = value;
        }

        public ObservableCollection<测试区域> 测试区域们
        {
            get => _测试区域们;
            set { _测试区域们 = value ?? new ObservableCollection<测试区域>(); }
        }

        public Drawing 出图()
        {
            var sw = new Stopwatch();
            sw.Start();

            if (测试区域们.Count == 0)
            {
                var run = DrawingHelper.GetGlyphRun("当前测试图层的子区域为空！", 26, Datar.Simhei, new Rect(0, 0, 200, 40),
                    out var w,
                    true);
                return new GlyphRunDrawing(Brushes.Gray, run);
            }

            var dg = new DrawingGroup();
            if (显示层格线)
            {
                var dg层格线 = new DrawingGroup();
                var gg横线 = new GeometryGroup();
                var gg竖线 = new GeometryGroup();
                for (int i = 0; i < 尺寸.纵长 + 1; i++)
                {
                    var ps = new Point(0, 尺寸.格长度 * i);
                    var pe = new Point(尺寸.像素大小.Width, 尺寸.格长度 * i);
                    gg横线.Children.Add(new LineGeometry(ps, pe));
                }

                for (int i = 0; i < 尺寸.横长 + 1; i++)
                {
                    var ps = new Point(尺寸.格长度 * i, 0);
                    var pe = new Point(尺寸.格长度 * i, 尺寸.像素大小.Height);
                    gg竖线.Children.Add(new LineGeometry(ps, pe));
                }

                var pen层格线 = new Pen(new SolidColorBrush(Color.FromArgb(135, 177, 177, 177)), 0.5);
                var gd横线 = new GeometryDrawing(null, pen层格线, gg横线);
                var gd竖线 = new GeometryDrawing(null, pen层格线, gg竖线);
                dg层格线.Children.Add(gd横线);
                dg层格线.Children.Add(gd竖线);
                dg.Children.Add(dg层格线);
            }

            foreach (var item in 测试区域们)
            {
                dg.Children.Add(item.出图());
            }

            sw.Stop();
            Datar.出图耗时 = sw.ElapsedMilliseconds;
            return dg;
        }
    }
}